<?php
namespace App\Exceptions;

use App\Constants\ErrorCode;
use Exception;
use Illuminate\Support\Facades\Log;
use Throwable;

class ServiceException extends Exception
{
    protected $code = ErrorCode::CODE_ERROR;
    /**
     * @var bool 是否记录日志
     */
    protected bool $recordLog = false;

    public function __construct($message = '', Throwable|null $previous = null, public $appendData = [])
    {
        if ($this->recordLog) {
            Log::warning(class_basename(static::class), [
                'message' => $message,
                'file' => $this->getFile() . ':' . $this->getLine(),
                'trace' => array_slice($this->getTrace(), 0, 5),
                'father_exception' => is_null($previous) ? null : format_exception($previous),
                'append_data' => $appendData,
            ]);
        }

        parent::__construct($message, $this->code, $previous);
    }
}
